/*
 * Copyright (C) 2010 Rodion Volkov (volkov.rodion@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rchik.divermate;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class DivingMenu extends AdvertisingActivity implements OnClickListener, OnDismissListener {

	static final int DIALOG_DIVING = 0;
	static final int DIALOG_REST = DIALOG_DIVING + 1;
	static final int DIALOG_SAFETY = DIALOG_DIVING + 2;
	static final int DIALOG_CALC_REST = DIALOG_DIVING + 3;
	static final int DIALOG_REST_REQUIRED = DIALOG_DIVING + 4;
	
	static final int REQUEST_DIVE = 1;
	static final int REQUEST_REST = 2;
	static final int CALC_REST = 3;
	
	private static int currentNitroLevel;
	private static int requiredRestTime;
	private static int currentTableId;
	
	private View topLayout;
	
	private static int selectedMetricType;
	private static ArrayList<MetricType> metricTypes = new ArrayList<MetricType>();
	private static ArrayList<DiveLogElement> diveLog;
	
	private static SafetyRequirement currentRequirement;
	private static DiveTablesDBAdapter mDbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.diving_menu);
		
		topLayout = this.findViewById(R.id.TopLayout);
		
		ImageButton button = (ImageButton)findViewById(R.id.MakeDiveButton);
		button.setOnClickListener(this);
		button = (ImageButton)findViewById(R.id.GetRestButton);
		button.setOnClickListener(this);
		button = (ImageButton)findViewById(R.id.CalculateRestButton);
		button.setOnClickListener(this);
		button = (ImageButton)findViewById(R.id.ClearNitroLevelButton);
		button.setOnClickListener(this);
		button = (ImageButton)findViewById(R.id.HistoryButton);
		button.setOnClickListener(this);
		button = (ImageButton)findViewById(R.id.DonateButton);
		button.setOnClickListener(this);
		findViewById(R.id.FacebookPostButton).setOnClickListener(this);
		
		currentNitroLevel = -1;
		requiredRestTime = -1;
		currentRequirement = new SafetyRequirement(0);
		currentTableId = getIntent().getExtras().getInt(DiveTablesDBAdapter.KEY_DIVINGTABLES_ROWID);
		
		mDbHelper = new DiveTablesDBAdapter(this);
		mDbHelper.open();
	}

	public DivingMenu() {
		super();

		diveLog = new ArrayList<DiveLogElement>();
		
		metricTypes.add(new MetricType(R.drawable.number_picker_m, 1, R.drawable.dive_history_m));
		metricTypes.add(new MetricType(R.drawable.number_picker_f, 3.2808f, R.drawable.dive_history_ft));
		
		selectedMetricType = 0;
	}
	
	public static ArrayList<MetricType> getMetricTypes()
	{
		return metricTypes;
	}

	public static int getSelectedMetricType()
	{
		return selectedMetricType;
	}
	
	public static void setSelectedMetricType(int value)
	{
		selectedMetricType = value;
	}
	
	public static void SetCurrentNitroLevel(int nitroLevel)
	{
		currentNitroLevel = nitroLevel;
	}
	
	public static void SetRequiredRestTime( int restTime )
	{
		requiredRestTime = restTime;
	}
	
	public static int getCurrentNitroLevel() {
		return currentNitroLevel;
	}

	public static SafetyRequirement getCurrentRequirement() {
		return currentRequirement;
	}
	
	public static int getCurrentTableId()
	{
		return currentTableId;
	}
	
	public static DiveTablesDBAdapter getmDbHelper() {
		return mDbHelper;
	}
	
	public static ArrayList<DiveLogElement> getDiveLog()
	{
		return diveLog;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		
		Dialog dialog;
		
		switch ( id )
		{
			case DIALOG_REST_REQUIRED:
			{
				dialog = new RequiredRestDialog(this);
				
				break;
			}
			default:
				
		        dialog = null;
		}
		return dialog;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		
		switch ( id )
		{
			case DIALOG_REST_REQUIRED:
			{
				((RequiredRestDialog)dialog).Initialize();
				((RequiredRestDialog)dialog).SetRequiredRestTime(requiredRestTime);
			}
			default:
				
		        dialog = null;
		}
	}

	@Override
	public void onClick(View arg0) {
		
		currentRequirement.setSafetyStopTime(0);
		
		switch ( arg0.getId())
		{
			case R.id.MakeDiveButton:
			{
				//showDialog(DIALOG_DIVING);
				Intent i = new Intent(this, DiveDepthTimeActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(i, REQUEST_DIVE);
				
				break;
			}
			case R.id.GetRestButton:
			{
				Intent i = new Intent(this, RestTimeActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(i, REQUEST_REST);
				
				break;
			}
			case R.id.CalculateRestButton:
			{
				Intent i = new Intent(this, TimeToRestBeforeDiveActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivityForResult(i, CALC_REST);
				
				break;
			}
			case R.id.ClearNitroLevelButton:
			{
				SetCurrentNitroLevel(-1);
				LevelIndicator.IndicateLevel(topLayout, currentNitroLevel);
				
				diveLog.clear();
				
				break;
			}
			case R.id.HistoryButton:
			{
				Intent i = new Intent(this, DiveLogActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(i);
				
				break;
			}
			case R.id.DonateButton:
			{
				DisplayDonatePage();
				break;
			}
			case R.id.FacebookPostButton:
			{
				Intent intent = new Intent(this, FacebookDataPostDialog.class);
				Bundle extras = new Bundle();
				extras.putInt("currentLevel", currentNitroLevel);
				intent.putExtras(extras);
				startActivity(intent);
				
				break;
			}
			default: break;
		}
	}

	public void DisplayDonatePage()
	{
		Intent i = new Intent(this, LoadDonation.class);
    	startActivity(i);
	}
	
	public static void SetCurrentSafetyRequirement(SafetyRequirement requirement) {
		currentRequirement = requirement;
	}

	@Override
	public void onDismiss(DialogInterface arg0) {
		LevelIndicator.IndicateLevel(topLayout, currentNitroLevel);
		
		if (currentRequirement.getSafetyStopTime() != 0)
		{
			showDialog(DIALOG_SAFETY);
			currentRequirement.setSafetyStopTime(0);
		}
		if ( requiredRestTime != -1 )
		{
			showDialog(DIALOG_REST_REQUIRED);
			requiredRestTime = -1;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
				
		LevelIndicator.IndicateLevel(topLayout, currentNitroLevel);
		
		if ( resultCode == Activity.RESULT_OK)
		{
			if (currentRequirement.getSafetyStopTime() != 0)
			{
				String timeToRest = ""+currentRequirement.getSafetyStopTime();
				
				Intent i = new Intent( this, SafetyWarningActivity.class );
				i.putExtra("time", timeToRest);
				i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
				
				currentRequirement.setSafetyStopTime(0);
			}
			if ( requiredRestTime != -1 )
			{
				showDialog(DIALOG_REST_REQUIRED);
				requiredRestTime = -1;
			}
		}
	}

}
